<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first" class="tab-pane">
            <!-- 表格部分 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addrols"
              >新增角色</el-button>
            </el-row>
            <!-- 表格部分 -->
            <el-table border style="width: 100%" :data="tableData">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="setRoles(scope.row)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRoles(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRoles(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页区域 -->
            <el-pagination
              :current-page="query.page"
              :page-sizes="[10, 15, 20, 25]"
              :page-size="query.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <!-- 面板:公司信息 -->
          <el-tab-pane label="公司信息" name="second">
            <el-alert
              title="消息提示的文案"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px" :model="companyinfo">
              <el-form-item label="公司名称">
                <el-input v-model="companyinfo.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyinfo.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyinfo.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyinfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
        <!-- 编辑添加角色弹框 -->
        <el-dialog
          :title="isedit?'编辑角色':'添加角色'"
          :visible.sync="dialogVisible"
          width="60%"
          :close-on-click-modal="false"
          @close="cancleRoles"
        >
          <!-- 表单部分 -->
          <template>
            <el-form ref="rolsform" :model="form" label-width="100px" :rules="rules">
              <el-form-item label="角色名称" prop="name">
                <el-input v-model="form.name" />
              </el-form-item>
              <el-form-item label="角色描述" prop="description">
                <el-input v-model="form.description" />
              </el-form-item>
            </el-form>
          </template>
          <span slot="footer" class="dialog-footer">
            <el-button @click="cancleRoles">取 消</el-button>
            <el-button type="primary" @click="roleSubmit">确 定</el-button>
          </span>
        </el-dialog>

        <!-- 分配角色弹窗 -->
        <el-dialog
          title="分配权限"
          :visible.sync="rluedialogVisible"
          width="60%"
          :close-on-click-modal="false"
          @close="rluedialogVisible=false"
        >
          <!-- 分配权限 -->
          <AssignPermission v-if="rluedialogVisible" :role-id="roleId" @close="rluedialogVisible=false" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getRoles, getCompanyInfo, addrolse, getrolse, editrolse, delrolse } from '@/api/setting'//
import AssignPermission from './assignPermission.vue'
export default {
  components: {
    AssignPermission
  },
  data() {
    return {
      activeName: 'first', // 默认显示tab页
      tableData: [], // 表格数据
      total: 0, // 总数
      query: { // 分页
        page: 1,
        pagesize: 10
      },
      companyinfo: {}, // 获取公司信息
      dialogVisible: false, // 控制弹窗显示隐藏
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }

        ],
        description: [
          { required: true, message: '请填写描述', trigger: 'blur' }
        ] },
      isedit: false, // 编辑或者添加状态
      rluedialogVisible: false, // 分配权限弹窗
      roleId: ''
    }
  },
  created() {
    this.getRoles()// 重新拉取角色列表
    this.getCompanyInfo()// 获取公司信息
  },
  methods: {
    // 控制页码显示条数
    handleSizeChange(newpagesize) {
      // 页码大小 每页多少条
      this.query.pagesize = newpagesize
      this.getRoles()
    //   console.log(this.tableData)
    },
    // 控制当前页面
    handleCurrentChange(newpage) {
      // 当前页面变动时候触发 当前页
      this.query.page = newpage
      this.getRoles()
    },
    // 分配权限
    setRoles(row) {
      this.rluedialogVisible = true
      this.roleId = row.id
    },
    // 点击编辑
    async editRoles(row) {
      this.isedit = true
      // 弹框出现.数据回填
      this.dialogVisible = true
      const res = await getrolse(row.id)
      this.form = res.data
    },
    // 删除
    async delRoles(row) {
      // 点击删除,出现确认弹窗
      const res = await this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (res === 'cancel') return this.$message('取消成功')
      const res1 = await delrolse(row.id)
      // 删除之后会有问题,最后一页删除最后一个,页面显示是空,需要判断页面数量的长度
      if (this.tableData.length === 1) {
        this.query.page--
        if (this.query.page <= 0) {
          this.query.page = 1
        }
      }
      this.$message.success(res1.message)
      this.getRoles()
    },
    // 获取角色信息
    async getRoles() {
      const res = await getRoles(this.query)
      this.tableData = res.data.rows
      this.total = res.data.total
    },
    // 获取公司信息
    async getCompanyInfo() {
      const id = this.$store.getters.companyId
      const res = await getCompanyInfo(id)
      //   console.log(res)
      this.companyinfo = res.data
    },
    // 点击新增角色
    addrols() {
      this.dialogVisible = true
      this.isedit = false
    },
    // 点击确定按钮 --兜底验证
    roleSubmit() {
      this.$refs.rolsform.validate(async(bool) => {
        if (!bool) return
        if (!this.isedit) { // 添加按钮
          const res = await addrolse(this.form)
          this.$message.success(res.message)
        } else { // 编辑按钮
          const res = await editrolse(this.form)
          this.$message.success(res.message)
        }

        // console.log(res.message)
        this.dialogVisible = false
        this.$refs.rolsform.resetFields()

        this.getRoles()
      })
    },
    // 点击取消按钮
    cancleRoles() {
      this.$refs.rolsform.resetFields()
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px;
}

.el-pagination {
  text-align: center;
  margin-top: 20px;
}</style>
